package com.wl.test.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.test.AbcMapper;
import com.wl.test.AbcRuquest;
import com.wl.test.entity.Abc;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wanlei
 * @since 2021/11/01
 */
@Service
public class AbcServiceImpl extends ServiceImpl<AbcMapper, Abc> implements AbcService{

    @Override
    public void updates(List<AbcRuquest> ruquest) {
        List<Integer> collect = ruquest.stream().map(item -> item.getId()).collect(Collectors.toList());
        List<Abc> abcs = baseMapper.selectBatchIds(collect);
        abcs.forEach(item -> {
            String extra = item.getExtra();
            JSONObject jsonObject = JSONObject.parseObject(extra);
            Map<String,Object> map = jsonObject;
            ruquest.forEach(abc -> {
                if(abc.getId().equals(item.getId())){
                    map.putAll(abc.getExtra());
                }
            });
            String string = JSON.toJSONString(map);
            item.setExtra(string);
        });
        baseMapper.updates(abcs);
    }

    @Override
    public List<Abc> abcAll(Set<String> id) {
        return baseMapper.findAll(id);
    }
}
